package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiPOQ extends Form implements FormOwi {

  OWIAgent owiAgent = null;
  ChoiceGroup msgContent = new ChoiceGroup("Message Content", ChoiceGroup.POPUP);
  TextField pointId = new TextField("Point Identifier", "", 16, TextField.ANY);
  TextField antHeight = new TextField("Antenna Height", "", 50,
                                      TextField.DECIMAL);
  ChoiceGroup antType = new ChoiceGroup("Antenna Type", ChoiceGroup.POPUP);

  public FormOwiPOQ(OWIAgent o) {
    super("Point Occupation Quit");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(msgContent);
    msgContent.append("Write point record", null);
    msgContent.append("Respond NMEA GGA", null);
    msgContent.append("Respond NMEA LLK", null);
    append(pointId);
    append(antHeight);
    antType.append("Vertical", null);
    antType.append("Slope", null);
    append(antType);
  }

  public void update() {
    // N/A
  }

  public void save() {

    String msgId = "OCC";

    // Setup SET parameters
    String[] outputs;

    if (msgContent.getSelectedIndex() == 0) {
      String[] temp = {
                    msgId, "1", pointId.getString(),
                    antHeight.getString(), "" + antType.getSelectedIndex()
      };
      outputs = temp;

    } else if (msgContent.getSelectedIndex() == 1) {
      String[] temp = {
              msgId, "2"
      };
      outputs = temp;

    } else {
      String[] temp = {
              msgId, "3"
      };
      outputs = temp;

    }
    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
